﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ServiceModel;
using System.ServiceModel.Channels;
namespace ExhibitionDraw.Server
{
    public class ServiceHelper
    {
       // public static string ServiceUrl =  "http://localhost:8083/WebService/CommService.svc";
       private static Service.CommServiceClient client = new Service.CommServiceClient();
        public static Service.CommServiceClient InitService()
        {
            if (client.State == System.ServiceModel.CommunicationState.Created)
            {
                var ServiceUrl = GetUrl() + "/WebService/CommService.svc";
                client.Endpoint.Address = new System.ServiceModel.EndpointAddress(ServiceUrl);
                return client;
            }
            else {
                return client;
            }

        }
        public static string GetUrl() {
            return App.Current.Host.Source.AbsoluteUri.Substring(0, App.Current.Host.Source.AbsoluteUri.IndexOf("/ClientBin"));
        }


        public static class WcfServiceClientFactory<TServiceClient, TService>
            where TServiceClient : ClientBase<TService>, TService
            where TService : class
        {
            public static TServiceClient CreateServiceClient()
            {
                var typeName = typeof(TService).Name;
                var serviceAddress = "../" + typeName + ".svc";
                return CreateServiceClient(serviceAddress);
            }

            public static TServiceClient CreateServiceClient(string serviceAddress)
            {
                var endpointAddr = new EndpointAddress(new Uri(Application.Current.Host.Source, serviceAddress));
                var binding = new BasicHttpBinding();
                var ctor = typeof(TServiceClient).GetConstructor(new Type[] { typeof(Binding), typeof(EndpointAddress) });
                return (TServiceClient)ctor.Invoke(new object[] { binding, endpointAddr });
            }
        }
    }
}
